package com.coffeeloversupportlibrary.coffeelover.domain.article.cotegory;

import android.os.Bundle;
import android.util.Log;
import android.view.View;

import androidx.annotation.Nullable;
import androidx.lifecycle.Observer;
import androidx.recyclerview.widget.StaggeredGridLayoutManager;
import androidx.viewpager.widget.ViewPager;

import com.coffeeloversupportlibrary.R;
import com.coffeeloversupportlibrary.coffeelover.domain.article.entity.Article;
import com.coffeeloversupportlibrary.coffeelover.uicompont.conflict.NestedScrollingParent2LayoutImpl3;
import com.coffeeloversupportlibrary.core.base.BaseFragment;
import com.coffeeloversupportlibrary.databinding.CategoryItemFragmnetBinding;

import java.util.List;
import java.util.Observable;

/**
 * @author puyv
 * @createTime 2021/11/22 15:16
 * @projectName CoffeeLoverSupportLibrary
 * @className CategoryFragmentTest.java
 * @description TODO
 *   // TODO: 2021/11/23    category  ==null??
 *                     // TODO: 2021/11/23    忽视了非常重要的问题    Binding 对象是父元素所持有的 父元素做的绑定 子元素直接持有的将是一个空引用
 * //                     categoryItemFragmnetBinding.categoryView.setAdapter(categoryListAdapter);
 * //                     categoryItemFragmnetBinding.categoryView.setLayoutManager(new StaggeredGridLayoutManager(2, StaggeredGridLayoutManager.VERTICAL));
 * //                     categoryListAdapter.setLists(articles);
 * //                     categoryListAdapter.notifyDataSetChanged();
 */
public class CategoryFragmentTest extends BaseFragment<CategoryViewModel, CategoryItemFragmnetBinding> {
    private int type;
    private NestedScrollingParent2LayoutImpl3 mNestedScrollingParent2Layout;
    private int mFragmentIndex;
    //todo  Fragment 持有 Adapter 懒改了
    private CategoryListAdapter categoryListAdapter;

    // TODO: 2021/11/23    bug 描述 文章页面和我的页面间的切换中 涉及到 Fragment 的回收   报错信息：  找不到构造器  如下解决方案
    public CategoryFragmentTest(){
        super();
    }

    public void setType(int type) {
        this.type = type;
    }

    //// TODO: 2021/11/23  数据请求分离核心逻辑
    @Override
    public void registerUIChangeCallBack() {
        switch (type){
            case 1: getViewModel().getCategoryOne().observe(this, new Observer<List<Article>>() {
                @Override
                public void onChanged(List<Article> articles) {
                    onchangeHandler(articles);
                }
            });
            break;


            case 2:getViewModel().getCategoryTwo().observe(this, new Observer<List<Article>>() {
                @Override
                public void onChanged(List<Article> articles) {
                    onchangeHandler(articles);
                }
            });
            break;


            case 3:getViewModel().getCategoryThree().observe(this, new Observer<List<Article>>() {
                @Override
                public void onChanged(List<Article> articles) {
                    onchangeHandler(articles);
                }
            });
            break;

            case 4:getViewModel().getCategoryFour().observe(this, new Observer<List<Article>>() {
                @Override
                public void onChanged(List<Article> articles) {
                    onchangeHandler(articles);
                }
            });
            break;

            case 5:getViewModel().getCategoryFive().observe(this, new Observer<List<Article>>() {
                @Override
                public void onChanged(List<Article> articles) {
                    onchangeHandler(articles);
                }
            });
            break;
        }
    }

    @Override
    public int getLayoutId() {
        return R.layout.category_item_fragmnet;
    }


    @Override
    public void initData(@Nullable Bundle savedInstanceState) {
        // TODO: 2021/11/22 虽然初始化在注册后调用 但onchage发生在初始化值改变后 所以在调用oncahnge前adapter!=null
        categoryListAdapter=new CategoryListAdapter();
        getBinding().categoryView.setAdapter(categoryListAdapter);
        getViewModel().initCategoryArticle(type);

    }

    // TODO: 2021/11/23  提供给 Onchange 调用 协助 回调
    private void onchangeHandler(List<Article> articles){
        if(null!=categoryListAdapter&&null!=articles){
            getBinding().categoryView.setLayoutManager(new StaggeredGridLayoutManager(2, StaggeredGridLayoutManager.VERTICAL));
            categoryListAdapter.setLists(articles);
            categoryListAdapter.notifyDataSetChanged();

        }
    }









    // TODO: 2021/11/22 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
    //todo:设计嵌套冲突相关代码
    @Override
    public void onResume() {
        super.onResume();
        Log.e("TAG","super.onResume();"+"true"+mFragmentIndex);
        if (isCurrentDisplayedFragment()) {
            if (mNestedScrollingParent2Layout != null) {
                mNestedScrollingParent2Layout.setChildRecyclerView(getBinding().categoryView);
            }
        }
    }
    /**
     * @date 2021/11/11 15:24
     * @params [visibleToUser]
     * @return void
     * @description TODO
     * 接口回调(因为该方法已过时)
     */
    @Override
    public void setUserVisibleHint(boolean visibleToUser) {
        super.setUserVisibleHint(visibleToUser);

        if (visibleToUser && isCurrentDisplayedFragment()) {
            if (mNestedScrollingParent2Layout != null) {
                mNestedScrollingParent2Layout.setChildRecyclerView(getBinding().categoryView);
            }
        }
    }

    public void setNestedParentLayout(NestedScrollingParent2LayoutImpl3 nestedScrollingParent2Layout) {
        mNestedScrollingParent2Layout = nestedScrollingParent2Layout;
    }

    public void setIndex(int i) {
        mFragmentIndex = i;
    }

    //当前Fragment 是否在设备上展示的判定
    private boolean isCurrentDisplayedFragment() {
        if (getView() == null || !(getView().getParent() instanceof View)) {
            return false;
        }
        View parent = (View) getView().getParent();
        if (parent instanceof ViewPager) {
            ViewPager viewPager = (ViewPager) parent;
            int currentItem = viewPager.getCurrentItem();
            return currentItem == mFragmentIndex;
        }
        return false;
    }


}
